import storage from 'store'
import { login, getInfo, logout } from '@/api/login'
import {
  UpdateInfo,
  updateUserPassword,
  getUserMenu,
  getHelpDocument,
  editHelpDcoument,
  getUserOverviewData
} from '@/api/user'
import {
  ACCESS_TOKEN,
  TOGGLE_NAV_THEME,
  TOGGLE_LAYOUT,
  TOGGLE_COLOR,
  SELECTED_ORG,
  REFRESH_TOKEN
} from '@/store/mutation-types'

const user = {
  state: {
    token: '',
    roles: [],
    orgs: [],
    info: {},
    userStyle: {},
    menu: [],
    selectedOrg: null,
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_ORGS:(state, orgs) => {
      state.orgs = orgs
    },
    SET_INFO: (state, info) => {
      state.info = info
    },
    SET_STYLE: (state, style) => {
      state.userStyle = style
    },
    SET_MENU: (state, menu) => {
      state.menu = menu
    },
    SET_ORG: (state,orgId) => {
      state.selectedOrg = orgId
    }
  },

  actions: {
    getHelpDoc () {
      return new Promise((resolve, reject) => {
        getHelpDocument()
          .then(res => {
            if (res.code==0) {
              resolve(res)
            } else {
              reject(res)
            }
          }).catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
    editHelpDoc ({}, content) {
      return new Promise((resolve, reject) => {
        editHelpDcoument(content)
          .then(res => {
            if (res.code==0) {
              resolve(res)
            } else {
              reject(res)
            }
          }).catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
    UpdateUserPassword ( { commit }, password ) {
      return new Promise((resolve, reject) => {
        updateUserPassword(password)
          .then(res => {
            if (res.code==0) {
              resolve(res)
            } else {
              reject(res)
            }
          }).catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
    // 更新个人信息
    UpdateUserInfo ( { commit }, userInfo ) {
      return new Promise((resolve, reject) => {
        UpdateInfo(userInfo)
          .then(res => {
            if (res.code==0) {
              commit('SET_INFO',userInfo)
              resolve(res)
            } else {
              reject(res)
            }
          }).catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },

    // 登录
    Login ({ commit, dispatch }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(res => {
          if (res.code == 0) {
            const result = res.data
            storage.set(ACCESS_TOKEN, result.access_token, result.expire_in)
            storage.set(REFRESH_TOKEN,result.refresh_token)
            commit('SET_TOKEN', result.access_token)
            resolve(res)
          } else {
            reject(res)
          }
        }).catch(error => {
          console.error(error)
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo ({ commit }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          if(res.code == 0){
            const data = res.data

            if (data.roles && data.roles.length > 0) {
              commit('SET_ROLES', data.roles)
              commit('SET_INFO', data)
              resolve(res)
            } else {
              reject(res)
            }
            if (data.orgs && data.orgs.length > 0){
              commit('SET_ORGS', data.orgs)
            }

          } else {
            reject(res)
          }
        }).catch(error => {
          console.error(error)
          reject(error)
        })
      })
    },
    // 获取用户菜单
    GetMenu({commit}) {
      return new Promise((resolve, reject) => {
        getUserMenu()
          .then(res => {
            if (res.code==0) {
              commit('SET_MENU',res.data)
              resolve(res)
            } else {
              reject(res)
            }
          }).catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },

    // 登出
    Logout ({ commit, state }) {
      return new Promise((resolve) => {
        commit('SET_TOKEN', '');
        commit('SET_ROLES', []);
        commit('SET_MENU', []);
        commit('SET_INFO',{});
        commit('SET_ORG',null);
        commit('SET_ORGS',[]);
        storage.remove(ACCESS_TOKEN);
        storage.remove(TOGGLE_NAV_THEME);
        storage.remove(TOGGLE_LAYOUT);
        storage.remove(TOGGLE_COLOR);
        storage.remove(SELECTED_ORG);
        resolve()
      })
    },
    getUserOverviewData ({ commit, state }) {
      return new Promise((resolve, reject) => {
        getUserOverviewData()
          .then(res => {
            if (res.code === 0) {
              resolve(res.data)
            } else {
              reject(res)
            }
          }).catch(error => {
          console.error(error)
          reject(error)
        })
      })
    }

  },
  getters:{
  }
}

export default user
